package crp.core.workflow.domain.expression.candidate;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
import crp.core.workflow.domain.expression.WorkflowExpressions;
import crp.core.workflow.domain.models.WorkflowCandidate;
import crp.core.workflow.domain.models.WorkflowCandidateType;
import crp.core.workflow.domain.services.WorkflowCandidateLoadContext;

import javax.enterprise.context.ApplicationScoped;
import java.util.Map;
import java.util.Set;

@ApplicationScoped
public class WorkflowCandidateSubmitterFunction extends AbstractFunction {
    public static final String NAME = "SUBMITTER";

    @Override
    public AviatorObject call(Map<String, Object> env) {
        WorkflowCandidateLoadContext context = WorkflowExpressions.context(env);
        return AviatorRuntimeJavaType.valueOf(Set.of(WorkflowCandidate.builder()
                .type(WorkflowCandidateType.USER)
                .target(context.getInstance().getCreatedBy())
                .build()));
    }

    @Override
    public String getName() {
        return NAME;
    }
}
